﻿using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.ProjectExtensions.Wizards.WssSchemaClasses;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TemplateWizard;
using Microsoft.VisualStudio.VSHelp;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace SPVSX.Project.Extensions.Wizards
{
    public static class WizardHelpers
    {
        #region Fields

        /// <summary>
        /// Field to back the DTE property
        /// </summary>
        internal static DTE _dte;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the Design Time Environment object (DTE)
        /// </summary>
        public static DTE Dte
        {
            get
            {
                if (_dte == null)
                {
                    _dte = (DTE)Package.GetGlobalService(typeof(DTE));
                }
                return _dte;
            }
            set
            {
                _dte = value;
            }
        }



        #endregion

        #region Methods

        /// <summary>
        /// Get the default project url
        /// </summary>
        /// <param name="designTimeEnvironment">The DTE object</param>
        /// <returns>The default site Uri</returns>
        public static Uri GetDefaultProjectUrl(DTE designTimeEnvironment)
        {
            Array activeSolutionProjects = designTimeEnvironment.ActiveSolutionProjects as Array;
            if ((designTimeEnvironment == null) || (activeSolutionProjects == null))
            {
                throw new WizardCancelledException();
            }
            foreach (object obj2 in activeSolutionProjects)
            {
                if (obj2 is EnvDTE.Project)
                {
                    EnvDTE.Project project = (EnvDTE.Project)obj2;
                    return BaseWizard.GetSharePointServices(project).Project.SiteUrl;
                }
            }
            throw new WizardCancelledException("Wizard Only Valid In SharePoint Project");
        }

        /// <summary>
        /// Check that the Uri is not null
        /// </summary>
        /// <param name="projectUrl">The project Uri</param>
        public static void CheckMissingSiteUrl(Uri projectUrl)
        {
            if (projectUrl == null)
            {
                //RtlAwareMessageBox.ShowError(null, WizardResources.SiteUrlMissing, WizardResources.ValidationErrorCaption);
                throw new WizardCancelledException("WizardResources.SiteUrlMissing");
            }
        }

        /// <summary>
        /// Get the active project
        /// </summary>
        /// <param name="designTimeEnvironment">The DTE object</param>
        /// <returns>The active project</returns>
        public static EnvDTE.Project GetActiveProject(DTE designTimeEnvironment)
        {
            if (designTimeEnvironment == null)
            {
                throw new ArgumentNullException("designTimeEnvironment");
            }
            EnvDTE.Project project = null;
            Array activeSolutionProjects = designTimeEnvironment.ActiveSolutionProjects as Array;
            if ((activeSolutionProjects != null) && (activeSolutionProjects.Length > 0))
            {
                project = activeSolutionProjects.GetValue(0) as EnvDTE.Project;
            }
            return project;
        }

        /// <summary>
        /// Get the active project name
        /// </summary>
        /// <param name="designTimeEnvironment">The DTE object</param>
        /// <returns>The active project name</returns>
        public static string GetActiveProjectName(DTE designTimeEnvironment)
        {
            //TODO: refactor into an extension on DTE and add resources
            if (designTimeEnvironment == null)
            {
                throw new ArgumentNullException("designTimeEnvironment");
            }
            EnvDTE.Project activeProject = GetActiveProject(designTimeEnvironment);
            if (activeProject == null)
            {
                throw new InvalidOperationException("WizardResources.ActiveProjectNotFoundError");
            }
            if (string.IsNullOrEmpty(activeProject.Name))
            {
                throw new InvalidOperationException("WizardResources.ActiveProjectNameInvalidError");
            }
            return activeProject.Name;
        }

        #endregion
    }
}
